var express = require('express');
var conn = require('./mysqlServer');
var operate = require('./mysqlOperate');
const md5 = require('nodejs-md5');

var router = express.Router();

// 登录
router.post('/login', function (req, res, next) {
    req.sql = "select * from login where account=?";
    req.data = [req.body.account];
    next();
}, operate.onlySearch, function (req, res, next) {
    if (req.result) {
        if (req.body.password == req.result.password) {
            md5.string.quiet(req.body.account + Math.floor(Math.random() * 10000), function (err, md5Str) {
                if (err) {
                    res.send(JSON.stringify({ code: 500, err: "登入失败" }))
                } else {
                    // 需要在这里写入token值写到数据库
                    let timestamp = new Date().getTime();
                    req.sql = "update login set token = ?, time= ? where account = ?";
                    req.data = [md5Str, timestamp, req.body.account];
                    conn.query(req.sql, req.data, function (err, result) {
                        if (err) {
                            res.send(JSON.stringify({ code: 500, err: err }));
                        }
                        res.send(JSON.stringify({ code: 200, token: md5Str }));
                    });
                }
            })
        } else {
            res.send(JSON.stringify({ code: 404, msg: "密码错误服务端" }));
        }
    } else {
        res.send(JSON.stringify({ code: 404, msg: "等着我帮你注册？" }));
    }
})

// 注册
router.post('/regist', function (req, res, next) {
    req.sql = "select * from login where account=?";
    req.data = [req.body.account];
    next();
}, operate.onlySearch, function (req, res, next) {
    if (req.result) {
        res.send(JSON.stringify({ code: 404, msg: "账号不存在服务端" }));
    } else {
        req.sql = "insert into login set ?";
        req.data = { account: req.body.account, password: req.body.password };
        conn.query(req.sql, req.data, function (err, result) {
            if (err) {
                res.send(JSON.stringify({ code: 500, err: "服务器内部错误" }));
            }
            res.send(JSON.stringify({ code: 200, msg: result }));
        })
    }
})


// 用来验证token码是否有效
router.post("/token", function (req, res, next) {
    // 获取token
    req.sql = "select * from login where token= ? ";
    req.data = [req.body.token];
    next();
}, operate.onlySearch, function (req, res, next) {
    console.log(req.result);
    if (req.result) {
        if (req.body.token) {
            let date = new Date();
            let date_old = req.body.time
            if (date.getTime() - date_old > 300000) {
                res.send(JSON.stringify({ code: 400, err: "未登录" }));
            } else {
                res.send(JSON.stringify({ code: 200, msg: "已登录" }));
            }
        } else {
            res.send(JSON.stringify({ code: 400, err: "未登录" }));
        }
    } else {
        res.send(JSON.stringify({
            code: 400,
            err: "没有登陆你个傻逼"
        }))
    }
})
// 查询数据
router.post("/select", function (req, res, next) {
    req.sql = "select * from login where account = ? ";
    req.data = [req.body.account];
    next();
}, operate.onlySearch, function (req, res, next) {
    res.send(JSON.stringify({ code: 200, msg: req.result }))
})
// 修改数据
router.post("/update", function (req, res, next) {
    req.sql = "select * from login where account = ? ";
    req.data = [req.body.username];
    next();
}, operate.onlySearch, function (req, res, next) {
    if (req.result) {
        req.sql = "update login set account = ? where account = ?";
        req.data = [req.body.account, req.body.username]
        conn.query(req.sql, req.data, function (err, result) {
            if (err) {
                res.send(JSON.stringify({ code: 500, err: "服务器内部错误" }));
            }
            res.send(JSON.stringify({ code: 200, msg: result }));
        })
    } else {
        res.send(JSON.stringify({ code: 404, msg: "账号不存在服务端" }));
    }
})
module.exports = router;